import { call, put, takeLatest } from 'redux-saga/effects';
import { REMOVE_BOOK,removeBook } from '../actions/books.action';
import Message from '../components/SimpleLayer/Message';
import API from '../services';
 
function showErr(content){
	Message.error({
		top:'6px',
		width:'300',
		content,
		delayAutoClose:2000,
		yes:false
	});
}
function* RemoveBook(action) {
   try {
      	const res = yield call(API.RemoveBook, action.data);
  		if (res.success) {
  			yield put(removeBook(action.data));
  			Message.success({
				top:'6px',
				width:'300',
				content:res.result,
				delayAutoClose:1200,
				yes:false
			});
		}else{
			showErr(res.result);
		}
   } catch (e) {
      	showErr(e.message);
   }
}

function* RemoveBookSaga() {
  yield takeLatest(REMOVE_BOOK, RemoveBook);
}

export default RemoveBookSaga;